package com.example.mobile2021606137xzt0511class;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity  {
    String urlString ="http://192.168.29.79:3000/posts/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvMainHTTp=findViewById(R.id.tvMainHttp);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL url =new URL(urlString);
                    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    InputStream inputStream=connection.getInputStream();//字节流 字符流 缓存流 ----)文字
                    InputStreamReader isr=new InputStreamReader(inputStream);//字符流
                    BufferedReader br=new BufferedReader(isr);//字符缓冲流
                    String temp=br.readLine();//读取1行文字

                    Log.i("TAG","run"+temp);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvMainHTTp.setText(temp);
                        }
                    });

                    br.close();;
                    isr.close();
                    inputStream.close();
                    connection.disconnect();


                    connection.disconnect();



                } catch (Exception e) {
                    e.printStackTrace();
                }





            }
        }.start();






    }
}